<template>
    <div class="user">
        <panel @add="addCoupon" @search="search"></panel>
        <couponList
            :couponList="couponList"
            class="list"
            @change="change($event); dialogFormVisible = true"
            @del="del"
        ></couponList>
        <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="Number(page_num)"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            class="pagination"
        ></el-pagination>
        <couponUpdate
            :dialogFormVisible="dialogFormVisible"
            @closeDialog="dialogFormVisible = false"
            :toChangeRow="toChangeRow"
            @refresh="refresh"
        ></couponUpdate>
        <couponAdd :dialogFormVisible="showAddDialog" @closeDialog="showAddDialog=false" @refresh="refresh" :addTemp="{...addTemp}"></couponAdd>
    </div>
</template>

<script>
import couponList from "@/components/coupon/couponList";
import panel from "@/components/panel";
import couponUpdate from "@/components/coupon/couponUpdate";
import couponAdd from '../../components/coupon/couponAdd';
export default {
    name: "Coupon",
    data() {
        return {
            couponList: [],
            page_now: undefined,
            total: undefined,
            page_num: undefined,
            currentPage:1,
            toChangeIndex: undefined,
            dialogFormVisible: false,
            toChangeRow: null,
            searchVal:'',
            showAddDialog:false,
            addTemp:{
                name: "",
                need_point: "",
                amount: "",
                status: ""
            }
        };
    },
    components: {
        couponList,
        panel,
        couponUpdate,
        couponAdd
    },
    methods: {
        addCoupon: function() {
            this.showAddDialog=true;
        },
        search: async function(val) {
            // alert(val);
            // /ycwAdmin/user/getUserList?key=12345;
            this.searchVal=val;
            let data = await this._axios
                .get(`/ycwAdmin/coupon/getCouponList?page=1&num=10&key=${val}`)
                .then(data => data.data.data);
            console.log(data);
            this.page_now = data.page_now;
            this.total = data.page_total * data.page_num;
            this.couponList = data.data;
            this.page_num = data.page_num;
        },
        handleCurrentChange: async function(val) {
            if(this.searchVal==''){
                let data = await this._axios
                .get(`/ycwAdmin/coupon/getCouponList?page=${val}&num=10`)
                .then(data => data.data.data);
                this.userList = data.data;
            }else{
                let data = await this._axios
                .get(`/ycwAdmin/coupon/getCouponList?page=${val}&num=10&key=${val}`)
                .then(data => data.data.data);
                this.userList = data.data;
            }
            
        },
        del: async function(row) {
            let data = await this._axios
                .get("/ycwAdmin/coupon/del?id=" + row.id)
                .then(data => data.data);
            console.log(data);
            if (data) {
                this.$alert(`${data.msg}`, {
                    confirmButtonText: "确定",
                    callback: () => {
                        this.$message({
                            type: "warning",
                            message: `${data.msg}`
                        });
                        this.refresh();
                    }
                });
            }
        },
        change: function(index) {
            // console.log({...row});
            this.toChangeRow = {...this.couponList[index]};
        },
        refresh: async function() {
            let data = await this._axios
                .get("/ycwAdmin/coupon/getCouponList?page=1&num=10")
                .then(data => data.data.data);
            console.log(data);
            this.page_now = data.page_now;
            this.total = data.page_total * data.page_num;
            this.couponList = data.data;
            this.page_num = data.page_num;
            this.currentPage=1;
        }
    },
    mounted: async function() {

        this.refresh();
    }
};
</script>

<style lang="scss" scoped>
.pagination {
    margin-top: 10px;
}
</style>